package com.cloudsigns.factorymanager2.engineering.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.IView.IAddCompletionOrderCommentView;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringDailyFeedBackBean;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringInstallationOrderBean;

import com.cloudsigns.factorymanager2.engineering.presenter.AddCompletionOrderCommentPresenter;
import com.cloudsigns.factorymanager2.project.activity.AddCompletionOfEvaluationActivity;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;
import com.cloudsigns.factorymanager2.project.utils.DateUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


/**
 * 工程安装完工单 超级管理员
 * Created by tony on 2018/5/11.
 */

public class EngineeringInstallCompletionOrderForSuperFragment extends Fragment implements View.OnClickListener, IAddCompletionOrderCommentView {
    private final String TAG = EngineeringInstallCompletionOrderForSuperFragment.class.getSimpleName();
    private TextView tv_project_name, tv_engineering_name;
    private View rl_project;
    private LinearLayout ll_engineering_info_content;
    private TextView edt_dispath_order_time_colon, edt_completionorder_date, edt_install_company, edt_install_responsible;
    private ImageView img_project_select;
    private ImageView img_add_evaluation;
    private TextView tv_construction_quality, tv_onstruction_schedule, tv_construction_environment, tv_construction_safe, tv_final_evaluation;
    private View rl_evalutaion;
    private TextView btn_confirm_evaluation;
    EngineeringInstallationOrderBean engineeringInstallationOrderBean;
    private String quality, schedule, environment, safe, final_evaluation;
    private boolean isFirst = true;
    private AddCompletionOrderCommentPresenter addCompletionOrderCommentPresenter;
    private boolean isClick = true;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_engineeringinstall_completionorder_for_super, null);
        tv_project_name = view.findViewById(R.id.tv_project_name);
        tv_engineering_name = view.findViewById(R.id.tv_engineering_name);
        rl_project = view.findViewById(R.id.rl_project);
        ll_engineering_info_content = view.findViewById(R.id.ll_engineering_info_content);
        edt_dispath_order_time_colon = view.findViewById(R.id.edt_dispath_order_time_colon);
        edt_completionorder_date = view.findViewById(R.id.edt_completionorder_date);
        edt_install_company = view.findViewById(R.id.edt_install_company);
        edt_install_responsible = view.findViewById(R.id.edt_install_responsible);

        img_project_select = view.findViewById(R.id.img_project_select);
        img_add_evaluation = view.findViewById(R.id.img_add_evaluation);
        btn_confirm_evaluation = view.findViewById(R.id.btn_confirm_evaluation);
        tv_construction_quality = view.findViewById(R.id.tv_construction_quality);
        tv_onstruction_schedule = view.findViewById(R.id.tv_onstruction_schedule);
        tv_construction_environment = view.findViewById(R.id.tv_construction_environment);
        tv_construction_safe = view.findViewById(R.id.tv_construction_safe);
        tv_final_evaluation = view.findViewById(R.id.tv_final_evaluation);
        rl_evalutaion = view.findViewById(R.id.rl_evalutaion);


        img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        rl_project.setOnClickListener(this);
        img_add_evaluation.setOnClickListener(this);
        btn_confirm_evaluation.setOnClickListener(this);

        addCompletionOrderCommentPresenter = new AddCompletionOrderCommentPresenter(this);

        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isFirst) {
            // 相当于onResume()方法
            initData();
            isFirst = false;
        } else {
            // 相当于onpause()方法
        }
    }


    public void setData(EngineeringInstallationOrderBean engineeringInstallationOrderBean) {
        this.engineeringInstallationOrderBean = engineeringInstallationOrderBean;
    }

    private void initData() {
        if (null != engineeringInstallationOrderBean) {
            tv_project_name.setText(engineeringInstallationOrderBean.getProjectName());
            tv_engineering_name.setText("/" + engineeringInstallationOrderBean.getEngineeringName());
            edt_dispath_order_time_colon.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getSendOrderTime()));
            edt_completionorder_date.setText(DateUtil.getYyyy_Mm_Dd(engineeringInstallationOrderBean.getCompleteDate()));
            edt_install_company.setText(engineeringInstallationOrderBean.getCompanyName());
            edt_install_responsible.setText(engineeringInstallationOrderBean.getReceiveAccountName());

            List<EngineeringDailyFeedBackBean> engineeringDailyFeedBackBeanList = engineeringInstallationOrderBean.getEngineeringDailyFeedbackList();
            if (null != engineeringDailyFeedBackBeanList) {
                for (EngineeringDailyFeedBackBean engineeringDailyFeedBackBean : engineeringDailyFeedBackBeanList) {
                    List<String> imgUrlList = engineeringDailyFeedBackBean.getImgUrlList(); //反馈上传图片地址
                    List<String> otherImgUrlList = engineeringDailyFeedBackBean.getImgUrlList();//其他图片列表
                }
            }
        }
    }

    private void showEngineeringInfoDialog() {
        if (ll_engineering_info_content.getVisibility() == View.VISIBLE) {
            ll_engineering_info_content.setVisibility(View.GONE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_down_icon);
        } else {
            ll_engineering_info_content.setVisibility(View.VISIBLE);
            img_project_select.setBackgroundResource(R.mipmap.arrow_top);
        }
    }

    private void submitCompletion() {
        addCompletionOrderCommentPresenter.addCompletionOrderComment();
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;
        switch (view.getId()) {

            case R.id.rl_project:
                showEngineeringInfoDialog();
                break;
            case R.id.img_add_evaluation:
                mIntent = new Intent(getActivity(), AddCompletionOfEvaluationActivity.class);
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                break;
            case R.id.btn_confirm_evaluation:
                if (isClick)
                    submitCompletion();
                break;

        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (data != null) {
                    Bundle mBundle = data.getExtras();
                    if (null != mBundle) {

                        quality = mBundle.getString("quality");
                        schedule = mBundle.getString("schedule");
                        environment = mBundle.getString("environment");
                        safe = mBundle.getString("safe");
                        final_evaluation = mBundle.getString("final_evaluation");

                        tv_construction_quality.setText(quality);
                        tv_onstruction_schedule.setText(schedule);
                        tv_construction_environment.setText(environment);
                        tv_construction_safe.setText(safe);
                        tv_final_evaluation.setText(final_evaluation);

                        rl_evalutaion.setVisibility(View.VISIBLE);


                    }

                }
                break;

        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public String getEvaluateInfo() {
        JSONObject jsonObj = new JSONObject();
        try {
            jsonObj.put("施工质量", quality);
            jsonObj.put("施工进度", schedule);
            jsonObj.put("施工环境", environment);
            jsonObj.put("施工安全", safe);
            jsonObj.put("综合评价", final_evaluation);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObj.toString();
    }

    @Override
    public String getOrderId() {
        if (null != engineeringInstallationOrderBean)
            return engineeringInstallationOrderBean.getId();
        return "";
    }

    @Override
    public void addCompletionOrderCommentSuccessed(String id) {
        Toast.makeText(getActivity(), "竣工成功", Toast.LENGTH_SHORT).show();
        btn_confirm_evaluation.setBackgroundColor(getResources().getColor(R.color.rgb_D9D9D9));
        isClick = false;
    }

    @Override
    public void addCompletionOrderCommentFailed(String desc) {
        Toast.makeText(getActivity(), desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
